<?php
//友情链接管理
namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests;

date_default_timezone_set("PRC");

class FriendlinkController extends CommonController
{
    //友情链接信息浏览
    public function index()
    {			
		$db = \DB::table("friendlink"); 
		//判断并封装搜索条件
		$params = array();
		if(!empty($_GET['name'])){
			$db->where("lname","like","%{$_GET['name']}%");
			$params['name'] = $_GET['name']; //维持搜索条件
		}
        $data = $db->orderBy('linkorder', 'asc')->paginate(5);
		return view("admin.friendlink.index",['list'=>$data,'params'=>$params]);
    }
	
	//加载添加页面
    public function create()
    {
        return view('admin.friendlink.add');
    }
	
	//添加链接
    public function store(Request $request)
    {	
		$pic = new \App\Org\Upload("logo");
		$pic->path="logo/"; 
		$pic->typeList =array("image/jpeg","image/png","image/gif"); 
		$pic->maxSize =0; 
		$res = $pic->doUpload();
		$linkInfo = ['lname'=>$request->lname,'url'=>$request->url,'logo'=>$res,'title'=>$request->title,'linkorder'=>$request->linkorder,'ltime'=>date("Y-m-d H:i:s",time()),'class'=>$request->clas];
		$id = \DB::table("friendlink")->insertGetId($linkInfo);	
		//判断
        if($id>0){
            $info = "友情链接添加成功！";
        }else{
            $info = "友情链接添加失败！";
        }	
        return view("admin.friendlink.add",['info'=>$info]);
    }
	
    public function show($id)
    {
        //
    }

    //加载修改页面
    public function edit($id)
    {
		$data = \DB::table("friendlink")->where("id",$id)->first();
        return view('admin.friendlink.edit',['list'=>$data]);
    }
	
	//修改链接
    public function update(Request $request, $id)
    {	
		if(!empty($request->logo)){
			//$logoName = \DB::table('friendlink')->select(\DB::raw('logo'))->where('id',$id)->first();
			$pic = new \App\Org\Upload("logo");
			$pic->path="logo/";
			$pic->typeList =array("image/jpeg","image/png","image/gif"); 
			$pic->maxSize =0;
			$res = $pic->doUpload();
			\DB::table("friendlink")->where('id',$id)->update(['logo'=>$res]);
		}
        $linkInfo = ['lname'=>$request->lname,'url'=>$request->url,'title'=>$request->title,'linkorder'=>$request->linkorder];
		\DB::table("friendlink")->where('id',$id)->update($linkInfo);
		return redirect("admin/friendlink/".$id."/edit");
    }
	
	//删除友情链接
    public function destroy($id)
    {	
        \DB::table('friendlink')->delete($id);
        return redirect("admin/friendlink");
    }
}
